﻿using System.Net.Http.Json;

namespace iOSClub.App.Share.Serves;

public class ProjectServer : IServer
{
    public ProjectServer()
    {
    }

    public ProjectServer(string jwt) : base(jwt)
    {
    }

    public ProjectServer(IServer server) : base(server)
    {
    }

    public async Task<List<ProjectModel>> GetAllData()
    {
        try
        {
            var res = await httpClient.GetAsync($"{_url}/api/Project/GetAllData");
            if (!res.IsSuccessStatusCode) return [];
            return await res.Content.ReadFromJsonAsync<List<ProjectModel>>() ?? [];
        }
        catch
        {
            return [];
        }
    }

    public async Task<List<ProjectModel>> GetYourProjects()
    {
        try
        {
            var res = await httpClient.GetAsync($"{_url}/api/Project/GetYourProjects");
            if (!res.IsSuccessStatusCode) return [];
            return await res.Content.ReadFromJsonAsync<List<ProjectModel>>() ?? [];
        }
        catch
        {
            return [];
        }
    }

    public async Task<List<TaskModel>> GetYourTasks()
    {
        try
        {
            var res = await httpClient.GetAsync($"{_url}/api/Project/GetYourTasks");
            if (!res.IsSuccessStatusCode) return [];
            return await res.Content.ReadFromJsonAsync<List<TaskModel>>() ?? [];
        }
        catch
        {
            return [];
        }
    }

    public async Task<ProjectModel?> CreateOrUpdateProject(ProjectModel model)
    {
        try
        {
            var res = await httpClient.PostAsJsonAsync($"{_url}/api/Project/CreateOrUpdateProject", model);
            if (!res.IsSuccessStatusCode) return null;
            return await res.Content.ReadFromJsonAsync<ProjectModel>();
        }
        catch
        {
            return null;
        }
    }

    public async Task<bool> DeleteProject(string id)
    {
        try
        {
            var res = await httpClient.GetAsync($"{_url}/api/Project/DeleteProject/{id}");
            return res.IsSuccessStatusCode;
        }
        catch
        {
            return false;
        }
    }

    public async Task<bool> LetChangeProject(string id, string projId)
    {
        try
        {
            var res = await httpClient.GetAsync($"{_url}/api/Project/LetChangeProject/{id}/{projId}");
            return res.IsSuccessStatusCode;
        }
        catch
        {
            return false;
        }
    }
    
    public async Task<TaskModel?> CreateOrUpdateTask(TaskModel model)
    {
        try
        {
            var res = await httpClient.PostAsJsonAsync($"{_url}/api/Project/CreateOrUpdateTask", model);
            if (!res.IsSuccessStatusCode) return null;
            return await res.Content.ReadFromJsonAsync<TaskModel>();
        }
        catch
        {
            return null;
        }
    }

    public async Task<bool> DeleteTask(string id)
    {
        try
        {
            var res = await httpClient.GetAsync($"{_url}/api/Project/DeleteTask/{id}");
            return res.IsSuccessStatusCode;
        }
        catch
        {
            return false;
        }
    }

    public async Task<TaskModel?> JoinTask(string id)
    {
        try
        {
            var res = await httpClient.GetAsync($"{_url}/api/Project/JoinTask/{id}");
            if (!res.IsSuccessStatusCode) return null;
            return await res.Content.ReadFromJsonAsync<TaskModel>();
        }
        catch
        {
            return null;
        }
    }
    
    public async Task<bool> LeaveTask(string id)
    {
        try
        {
            var res = await httpClient.GetAsync($"{_url}/api/Project/LeaveTask/{id}");
            return res.IsSuccessStatusCode;
        }
        catch
        {
            return false;
        }
    }
    
    public async Task<bool> LetChangeTask(string id, string projId)
    {
        try
        {
            var res = await httpClient.GetAsync($"{_url}/api/Project/LetChangeTask/{id}/{projId}");
            return res.IsSuccessStatusCode;
        }
        catch
        {
            return false;
        }
    }
}